#!/bin/sh

FLAGS='-l chat.log'
MAILTO='your@email.here'
TAG='New StormChat Install'

if [ $MAILTO = 'your@email.here' ]; then
	echo "You need to edit this script and set your email address first!"
	exit
fi

if [ ! -r bin/sbchat ]; then
	echo "You need to compile StormChat before trying to run it!"
	exit
fi

while ( : ) do
	DATE=`date +%m.%d.%H.%M.%S`

	#
	# Rotate log and core file
	#
	if [ -r chat.log ]; then
		mv chat.log logs/chat.log.$DATE
	fi

	mv core.* cores/core.$DATE

	if [ -r cores/core.$DATE ]; then
		#
		# Perform quick automated analysis of the core file and email it off
		#
		gdb bin/sbchat cores/core.$DATE < gdboptions  >> cores/core.$DATE.log
		mail -s "[$TAG] Chat Coredump Report" $MAILTO < cores/core.$DATE.log
	fi

	#
	# Restart chat...
	#
	echo "Restarted chat: $DATE"
	
	bin/sbchat $FLAGS

	if [ -r .killscript ]; then
		DATE=`date`;
		echo "autoscript killed $DATE"  
		rm .killscript
		exit
	fi
done
